﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace RM2012Mobile
{
    public class ImagePanel : Panel
    {
        private Image image;
        //private bool bPushed;
        private Bitmap m_bmpOffscreen;
        private PanelSizeMode sizeModeValue = PanelSizeMode.Normal;

        public Image Image
        {
            get
            {
                return image;
            }
            set
            {
                image = value;
            }
        }

        public PanelSizeMode SizeMode
        {
            get
            {
                return sizeModeValue;
            }
            set
            {
                if (value != sizeModeValue)
                {
                    sizeModeValue = value;
                    this.Invalidate();
                }
            }
        }

        public ImagePanel()
        {
            //bPushed = false;
            //default minimal size
            this.Size = new Size(21, 21);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics gxOff;	   //Offscreen graphics
            Rectangle imgRect; //image rectangle
            Brush backBrush;   //brush for filling a backcolor

            if (m_bmpOffscreen == null) //Bitmap for doublebuffering
            {
                m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
            }

            gxOff = Graphics.FromImage(m_bmpOffscreen);

            gxOff.Clear(this.BackColor);

            backBrush = new SolidBrush(Parent.BackColor);

            gxOff.FillRectangle(backBrush, this.ClientRectangle);
            if (image != null)
            {
                //Center the image relativelly to the control
                int imageLeft = 0;
                int imageTop = 0;
                if (SizeMode == PanelSizeMode.Normal)
                {
                    imageLeft = (this.Width - image.Width) / 2;
                    imageTop = (this.Height - image.Height) / 2;
                }
                else if (SizeMode == PanelSizeMode.StretchImage)
                {
                    imageLeft = 0;
                    imageTop = 0;
                }
                //Set temporary
                imgRect = new Rectangle(imageLeft, imageTop, this.Width, this.Height);

                if (SizeMode == PanelSizeMode.Normal)
                {
                    imgRect = new Rectangle(imageLeft, imageTop, image.Width, image.Height);
                }
                else if (SizeMode == PanelSizeMode.StretchImage)
                {
                    imgRect = new Rectangle(imageLeft, imageTop, this.Width, this.Height);
                }

                //Set transparent key
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetColorKey(BackgroundImageColor(image), BackgroundImageColor(image));
                //Draw image
                //
                gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
            }

            //Draw from the memory bitmap
            e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);

            base.OnPaint(e);
        }

        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        {
            //Do nothing
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            this.Invalidate();
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            this.Invalidate();
        }

        private Color BackgroundImageColor(Image image)
        {
            Bitmap bmp = new Bitmap(image);
            return bmp.GetPixel(0, 0);
        }
    }

    public enum PanelSizeMode
    {
        Normal,
        StretchImage
    }
}
